package com.h3c.tj_nsh.Utils;

/**
 * Time: 2016/5/13.
 * Author:Chen  .
 */

import android.content.Context;

import com.squareup.okhttp.Cache;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * OkHttpClient自定义工具类
 */
public class OkHttpUtils {

    private static OkHttpClient singleton;

    public static OkHttpClient getInstance(Context context) throws IOException, ClassNotFoundException {
        if (singleton == null) {
            synchronized (OkHttpUtils.class) {
                if (singleton == null) {
                    File cacheDir = new File(context.getCacheDir(), "http");
                    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
                    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                    singleton = new OkHttpClient();
                    long httpCacheSize = 5 * 1024 * 1024;
                    singleton.setCache(new Cache(cacheDir, httpCacheSize));
                    singleton.setConnectTimeout(5, TimeUnit.SECONDS);
                    singleton.setReadTimeout(5, TimeUnit.SECONDS);
                    singleton.interceptors().add(interceptor);
                    singleton.interceptors().add(new AuthorizationInterceptor("admin","admin"));
                }
            }
        }
        return singleton;
    }
}